/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package th.manager;

import com.google.inject.Inject;
import com.wideplay.warp.persist.Transactional;
import org.hibernate.Query;
import org.hibernate.Session;
import th.dao.AkaunDAO;
import th.dao.KodKebenaranTransaksiDAO;
import th.model.Akaun;
import th.model.AkaunFav;
import th.model.KodKebenaranTransaksi;
import th.model.Pengguna;
import th.dao.AkaunFavDAO;

/**
 *
 * @author ubuntu
 */
public class DepositManager {
    
    @Inject
    protected com.google.inject.Provider<Session> sessionProvider;
    @Inject 
    private AkaunDAO akaunDAO;
    @Inject 
    private AkaunFavDAO akaunFavDAO;
    @Inject 
    private KodKebenaranTransaksiDAO kodKebenaranTransaksiDAO;
    
    @Transactional
    public void saveAccount(Akaun akaun) {
        akaunDAO.save(akaun);
    }    
    
    @Transactional
    public void saveAccountFav(AkaunFav akaunFav) {
       akaunFavDAO.save(akaunFav);
    }   
    
    public boolean authenticate(String noAkaun, String noPengenalan) {
//        String encrypted = encrypt(password);
        Session session = sessionProvider.get();
        Query query = session.createQuery(
                "select a from Akaun a " +
                "where akaunTmbhn = :noAkaun and noPengenalanTmbhn = :noPengenalan");
        query.setString("noAkaun", noAkaun);
        query.setString("noPengenalan", noPengenalan);

        Akaun akaun = (Akaun) query.uniqueResult();
        return akaun != null;
    }
    
    public boolean childAccAuthenticate(String noAkaun, String noKpPenjaga) {
//        String encrypted = encrypt(password);
        Session session = sessionProvider.get();
        Query query = session.createQuery(
                "select a from Akaun a " +
                "where akaunTmbhn = :noAkaun and a.pengguna.noKp = :noKpPenjaga");
        query.setString("noAkaun", noAkaun);
        query.setString("noKpPenjaga", noKpPenjaga);
        Akaun akaun = (Akaun) query.uniqueResult();
        return akaun != null;
    }
        
    @Transactional
    public void saveTAC(KodKebenaranTransaksi kodKebenaranTransaksi) {
        kodKebenaranTransaksiDAO.save(kodKebenaranTransaksi);
    }
    
    @Transactional
    public void deleteTAC(KodKebenaranTransaksi kodKebenaranTransaksi) {
        kodKebenaranTransaksiDAO.delete(kodKebenaranTransaksi);
    }
    
    public KodKebenaranTransaksi findKodKebenaranTransaksiByPengguna(Pengguna pguna, String operasi) {        
        String query = "SELECT a FROM KodKebenaranTransaksi a WHERE a.infoAudit.dimasukOleh= :pguna AND a.operasi = :operasi";
        KodKebenaranTransaksi q = (KodKebenaranTransaksi)sessionProvider.get().createQuery(query).setString("operasi", operasi.toString()).uniqueResult();
        return q;
    } 
    
}


